﻿package src.software 
{
	import flash.display.Shape;
	import flash.events.Event;
	import flash.geom.Point;
	import src.interfaces.IMeggyJrRGB;
	
	/**
	* ...
	* @author Louis Tovar
	*/
	public class MarqueeTest extends Shape
	{
		
		private var meggyJr:IMeggyJrRGB;
		
		private var currentPixel:Point;
		
		private var tinyFont:Array;
		
		public function MarqueeTest(p_meggyHardware:IMeggyJrRGB) 
		{
			initTinyFont();
			
			currentPixel = new Point();
			
			meggyJr = p_meggyHardware;
			
			currentPixel.x = 3;
			currentPixel.y = 3
			
			
			writeLetter('A', 0, 0, 0xFF00FF);
			writeLetter('B', 3, 4, 0xB4AD0A);
			writeLetter('C', 3, 0, 0xAD0BEE);
			
			meggyJr.mainDisplay.ShowScreen();
			
			//meggyJr.mainDisplay.DrawPx(currentPixel.x, currentPixel.y, 0xFF0000);
			//meggyJr.mainDisplay.ShowScreen();
			
			//addEventListener(Event.ENTER_FRAME, animatePixel);
		}
		
		private function initTinyFont():void
		{
			tinyFont = new Array();
			
			/*
			tinyFont['A'] = [
								[0, 0, 0, 0],
								[0, 0, 0, 0],
								[0, 0, 0, 0],
								[0, 0, 0, 0],
								[0, 0, 0, 0]
							]
			*/
			
			
			// tiny fonts are 4x5
			tinyFont['A'] = [
								[0, 1, 0, 0],
								[1, 0, 1, 0],
								[1, 1, 1, 0],
								[1, 0, 1, 0],
								[0, 0, 0, 0]
							]
							
			tinyFont['B'] = [
								[1, 1, 0, 0],
								[1, 1, 1, 0],
								[1, 0, 1, 0],
								[1, 1, 0, 0],
								[0, 0, 0, 0]
							]
							
			tinyFont['C'] = [
								[0, 1, 1, 0],
								[1, 0, 0, 0],
								[1, 0, 0, 0],
								[0, 1, 1, 0],
								[0, 0, 0, 0]
							]
		}
		
		private function writeLetter(p_letter:String, p_x:uint, p_y:uint, p_color:uint):void
		{
			if (tinyFont[p_letter])
			{
				for (var rowIndex:uint = 0; rowIndex < tinyFont[p_letter].length; rowIndex++)
				{
					if (p_y + rowIndex > 7)
					{
						break;
					}
					while (p_y + rowIndex < 0)
					{
						p_y++;
						if (p_y + rowIndex >= tinyFont[p_letter].length)
						{
							break;
						}
					}
					
					for (var columnIndex:uint = 0; columnIndex < tinyFont[p_letter][rowIndex].length; columnIndex++)
					{
						if (p_x + columnIndex > 7)
						{
							break;
						}
						
						while (p_x + columnIndex < 0)
						{
							p_x++;
							if (p_x + columnIndex >= tinyFont[p_letter][rowIndex].length)
							{
								break;
							}
						}
						
						if (tinyFont[p_letter][rowIndex][columnIndex])
						{
							meggyJr.mainDisplay.DrawPx(p_x + columnIndex, p_y + rowIndex, p_color);
						}
						else
						{
							meggyJr.mainDisplay.DrawPx(p_x + columnIndex, p_y + rowIndex, 0x303030);
						}
					}
					
				}
			}
		}
		
		private var previousPixel:Point;
		private function animatePixel(e:Event):void 
		{
			previousPixel = new Point();
			previousPixel.x = currentPixel.x;
			previousPixel.y = currentPixel.y;
			
			currentPixel.x += (int(Math.random()*2) - 1 ) ? 1 : -1;
			currentPixel.y += (int(Math.random()*2) - 1 ) ? 1 : -1;
			
			if (currentPixel.x < 0)
			{
				currentPixel.x = 7;
			}
			
			if (currentPixel.y < 0)
			{
				currentPixel.y = 7;
			}
			
			if (currentPixel.x > 7)
			{
				currentPixel.x = 0;
			}
			
			if (currentPixel.y > 7)
			{
				currentPixel.y = 0;
			}
			
			meggyJr.mainDisplay.DrawPx(previousPixel.x, previousPixel.y, 0x303030);
			meggyJr.mainDisplay.DrawPx(currentPixel.x, currentPixel.y, meggyJr.mainDisplay.ReadPx(previousPixel.x, previousPixel.y));
			
			meggyJr.mainDisplay.ShowScreen();
			
			
		}
		
	}
	
}